/*
 * support3
 */
package com.pitop.frontend.ctrl;

import com.alibaba.fastjson.JSON;
import com.pitop.core.entity.BeanRet;
import com.pitop.core.entity.Page;
import com.pitop.core.exceptions.BaseException;
import com.pitop.core.exceptions.FrontendI18nCategoryException;
import com.pitop.core.tools.StringTools;
import com.pitop.core.tools.redis.RedisUtils;
import com.pitop.frontend.entity.FrontendI18nCategory;
import com.pitop.frontend.service.FrontendI18nCategorySV;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 前端国际化分类 控制器
 *
 * @author support3
 */
@RestController
@RequestMapping("/frontendI18nCategory")
@Slf4j
@Api(value = "前端国际化分类控制器", description = "前端国际化分类控制器")
public class FrontendI18nCategoryCtrl {

    @Resource
    protected RedisUtils redisUtils;

    @Resource
    private FrontendI18nCategorySV frontendI18nCategorySV;


    /**
     * 根据条件id查询FrontendI18nCategory一个详情信息
     *
     * @param id id
     * @return BeanRet
     */
    @ApiOperation(value = "查询FrontendI18nCategory一个详情信息", notes = "查询FrontendI18nCategory一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", dataType = "java.lang.Long", paramType = "path")
    })
    @GetMapping(value = "/load/id/{id}")
    @ResponseBody
    public FrontendI18nCategory loadById(@PathVariable Long id) {
        if (id == null) {
            return null;
        }
        FrontendI18nCategory frontendI18nCategory = frontendI18nCategorySV.loadById(id);
        log.info(JSON.toJSONString(frontendI18nCategory));
        return frontendI18nCategory;
    }

    /**
     * 根据条件code查询FrontendI18nCategory一个详情信息
     *
     * @param code 编码
     * @return BeanRet
     */
    @ApiOperation(value = "查询FrontendI18nCategory一个详情信息", notes = "查询FrontendI18nCategory一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "编码", dataType = "java.lang.String", paramType = "path")
    })
    @GetMapping(value = "/load/code/{code}")
    @ResponseBody
    public FrontendI18nCategory loadByCode(@PathVariable String code) {
        if (code == null) {
            return null;
        }
        FrontendI18nCategory frontendI18nCategory = frontendI18nCategorySV.loadByCode(code);
        log.info(JSON.toJSONString(frontendI18nCategory));
        return frontendI18nCategory;
    }


    /**
     * 查询FrontendI18nCategory信息集合
     *
     * @return 分页对象
     */
    @ApiOperation(value = "查询FrontendI18nCategory信息集合", notes = "查询FrontendI18nCategory信息集合")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "curPage", value = "当前页", required = true, paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", required = true, paramType = "query"),
    })
    @GetMapping(value = "/list")
    @ResponseBody
    public BeanRet list(@ApiIgnore FrontendI18nCategory frontendI18nCategory, Integer curPage, Integer pageSize) {
        Page<FrontendI18nCategory> page = new Page<>(pageSize, curPage);
        List<FrontendI18nCategory> frontendI18nCategorys = frontendI18nCategorySV.list(frontendI18nCategory, page.genRowStart(), page.getPageSize());
        page.setVoList(frontendI18nCategorys);
        int total = frontendI18nCategorySV.count(frontendI18nCategory);
        page.setTotalRow(total);
        log.info(JSON.toJSONString(page));
        return BeanRet.create(true, "查询成功", page);
    }


    /**
     * 查询FrontendI18nCategory信息集合
     *
     * @return 分页对象
     */
    @ApiOperation(value = "查询FrontendI18nCategory信息集合", notes = "查询FrontendI18nCategory信息集合")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "curPage", value = "当前页", required = true, paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", required = true, paramType = "query"),
            @ApiImplicitParam(name = "code", value = "编码", paramType = "query")
    })
    @GetMapping(value = "/list/by")
    @ResponseBody
    public List<FrontendI18nCategory> listByPk(Long id, String code, Integer curPage, Integer pageSize) {
        Page<FrontendI18nCategory> page = new Page<FrontendI18nCategory>(pageSize, curPage);
        List<FrontendI18nCategory> frontendI18nCategorys = frontendI18nCategorySV.list(id, code, page.genRowStart(), page.getPageSize());
        int total = frontendI18nCategorySV.count(id, code);
        page.setTotalRow(total);
        log.info(JSON.toJSONString(page));
        return frontendI18nCategorys;
    }

    /**
     * 统计FrontendI18nCategory信息数量
     *
     * @return 总条数
     */
    @ApiOperation(value = "统计FrontendI18nCategory信息数量", notes = "统计FrontendI18nCategory信息数量")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "国际化分类名", paramType = "query"),
            @ApiImplicitParam(name = "prefix", value = "国际化分类编码前缀", paramType = "query"),
            @ApiImplicitParam(name = "state", value = "状态(1012)：启用 Enable,禁用 Disable", paramType = "query"),
            @ApiImplicitParam(name = "summary", value = "说明 128个字以内", paramType = "query")
    })
    @GetMapping(value = "/count")
    @ResponseBody
    public Integer count(@ApiIgnore FrontendI18nCategory frontendI18nCategory) {
        return frontendI18nCategorySV.count(frontendI18nCategory);
    }


    /**
     * 创建FrontendI18nCategory
     *
     * @return BeanRet
     */
    @ApiOperation(value = "创建FrontendI18nCategory", notes = "创建FrontendI18nCategory")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "国际化分类名", paramType = "query", required = true),
            @ApiImplicitParam(name = "prefix", value = "国际化分类编码前缀", paramType = "query", required = true),
            @ApiImplicitParam(name = "summary", value = "说明 128个字以内", paramType = "query")
    })
    @PostMapping("/build")
    @ResponseBody
    public BeanRet build(@ApiIgnore FrontendI18nCategory frontendI18nCategory) {
        frontendI18nCategorySV.save(frontendI18nCategory);
        return BeanRet.create(true, "添加成功");
    }


    /**
     * 修改前端国际化分类对象
     * 1.参数合法性验证
     * ****非空判断
     * ****国际化分类名是否存在
     * ****国际化分类前缀是否存在
     * 2.修改数据
     *
     * @return BeanRet
     */
    @ApiOperation(value = "修改FrontendI18nCategory", notes = "修改FrontendI18nCategory")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "编码", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "国际化分类名", paramType = "query"),
            @ApiImplicitParam(name = "prefix", value = "国际化分类编码前缀", paramType = "query"),
            @ApiImplicitParam(name = "state", value = "状态(1012)：启用 Enable,禁用 Disable", paramType = "query"),
            @ApiImplicitParam(name = "summary", value = "说明 128个字以内", paramType = "query")
    })
    @PutMapping("/modify")
    @ResponseBody
    public BeanRet modify(@ApiIgnore FrontendI18nCategory frontendI18nCategory) {
        //1.参数合法性验证，非空判断
        if (frontendI18nCategory == null) {
            throw new FrontendI18nCategoryException(BaseException.BaseExceptionEnum.Empty_Param);
        }
        if (StringTools.isEmpty(frontendI18nCategory.getName())) {
            throw new FrontendI18nCategoryException(BaseException.BaseExceptionEnum.Empty_Param);
        }
        if (StringTools.isEmpty(frontendI18nCategory.getPrefix())) {
            throw new FrontendI18nCategoryException(BaseException.BaseExceptionEnum.Empty_Param);
        }
        //国际化分类名是否存在
        Map<String, Object> params = new HashedMap();
        params.put("name", frontendI18nCategory.getName());
        FrontendI18nCategory frontendI18nCategoryFlag = frontendI18nCategorySV.load(params);
        if (frontendI18nCategoryFlag != null && !frontendI18nCategory.getCode().equals(frontendI18nCategoryFlag.getCode())) {
            throw new FrontendI18nCategoryException(BaseException.BaseExceptionEnum.Exists);
        }
        //前缀是否是英文
        if(!StringTools.checkEnlish(frontendI18nCategory.getPrefix())) {
            throw new FrontendI18nCategoryException(BaseException.BaseExceptionEnum.Must_Be_In_English);
        }
        //国际化分类前缀是否存在
        params = new HashedMap();
        params.put("prefix", frontendI18nCategory.getPrefix());
        frontendI18nCategoryFlag = frontendI18nCategorySV.load(params);
        if (frontendI18nCategoryFlag != null && !frontendI18nCategory.getCode().equals(frontendI18nCategoryFlag.getCode())) {
            throw new FrontendI18nCategoryException(BaseException.BaseExceptionEnum.Exists);
        }
        frontendI18nCategorySV.modify(frontendI18nCategory);
        return BeanRet.create(true, "修改成功");
    }

}
